<?php
/**
 * Expected it that the class Article is already required by the script 
 * requiring this class
 */

require_once "Database.php";

class ArticlesDb extends Database {
	protected $sortBy;	
	
	public function __construct() {
		parent::__construct();
		
		$this->sortBy = "distance";
	}
	
	public function add($art) {
		$art->latitude = 2;
		$art->longtitude = 2;
	
		$query = "
			INSERT INTO ".$this->c->c['tables']['articles']."
			VALUES (DEFAULT, '".$art->username."', '".addslashes($art->subject)."', 
			        '".addslashes($art->text)."', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, 
			        ".$art->latitude.",".$art->longtitude.",'".$art->image."')";
		$this->query($query);
	}
	
	public function update($art) {
		$query = "
			UPDATE ".$this->c->c['tables']['articles']."
			SET subject='".$art->subject."', text='".$art->text."',
			    date='".$art->date."', updated=CURRENT_TIMESTAMP, 
			    image='".$art->image."'
			WHERE aid=".$art->aid;
		$this->query($query);
	}
	
	public function get($amount, $aid = null, $offset = null) {
		if (!is_numeric($amount))
			return false;
		
		$query  = "
			SELECT * FROM ".$this->c->c['tables']['articles'];
		if ($aid != null)
			$query .= "
				WHERE aid=".$aid;
		$query .= "
			ORDER BY updated DESC
			LIMIT ".$amount;
		if ($offset != null)
			$query .= "
				OFFSET (".$offset.")";
		
		return $this->query($query);
	}
	
	public function delete($art) {
		if (!is_numeric($art->aid))
			return false;
		
		$result = $this->get(1, $art->aid);
		$result = $this->result2array($result);
		$this->delArtImg($result[0]['image']);
		
		$query = "
			DELETE FROM ".$this->c->c['tables']['articles']."
			WHERE aid=".$art->aid;
		$this->query($query);
	}
	
	public function getByAuthor($amount, $user) {
		if (!is_numeric($amount))
			return false;
		
		$query = "
			SELECT * FROM ".$this->c->c['tables']['articles']."
			WHERE username='".strtolower($user->name)."'
			ORDER BY date DESC
			LIMIT ".$amount;
		$articles = $this->query($query);
		$articles = $this->result2array($articles);
		
		for ($i = 0; $i < count($articles); $i++) {
			$content[$i] = new Article($articles[$i]);
			$content[$i]->user = $user->name;
		}
		
		return $content;
	}
	
	public function search($search, $pag) {
		
		$query = "SELECT * FROM ".$this->c->c['tables']['articles']." 
		          WHERE (subject  LIKE '%".$search[0]."%'
       	                      OR text     LIKE '%".$search[0]."%'
	                      OR username LIKE '%".$search[0]."%')";
			  for ($i = (count($search)-1); $i>0; $i--) {
	                  	$query .= " AND (subject  LIKE '%".$search[$i]."%'
	                                      OR text     LIKE '%".$search[$i]."%'
	                                      OR username LIKE '%".$search[$i]."%')";
	                  }
	        $query .= " ORDER BY updated DESC LIMIT 8 OFFSET (".$pag."*8)";
		
		
		$count = "SELECT COUNT(*) FROM ".$this->c->c['tables']['articles']." 
		          WHERE (subject  LIKE '%".$search[0]."%'
       	                      OR text     LIKE '%".$search[0]."%'
	                      OR username LIKE '%".$search[0]."%')";
			  for ($i = (count($search)-1); $i>0; $i--) {
	                  	$count .= " AND (subject  LIKE '%".$search[$i]."%'
	                                      OR text     LIKE '%".$search[$i]."%'
	                                      OR username LIKE '%".$search[$i]."%')";
	                  }
	        
	        $articles = $this->query($query);
	        $articles = $this->result2array($articles);
	        if($articles == null) {
	        	header("Location: search.php?err=no_results");
				die();
	        }
	        $ammount = $this->query($count);
		$ammount = $this->result2array($ammount);
		for($i = 0; $i < count($articles); $i++) {
			$articles[$i]['max_articles'] = $ammount[0]['count'];
		}
		return $articles;
	}
	
	public function delArtImg($loc) {
		$loc = $this->c->c['dirs']['base_abs']."/img/pub/".$loc;
	
		if (file_exists($loc."s.jpg"))
			unlink($loc."s.jpg");
		
		if (file_exists($loc.".jpg"))
			unlink($loc.".jpg");
	}
	
	public function getNrArticles() {
		$query = "SELECT COUNT(*) FROM ".$this->c->c['tables']['articles'];
		$nr = $this->query($query);
		$nr = $this->result2array($nr);
		return $nr[0]['count'];
	}
}
?>
